package cn.josh.daolmpl;

import java.sql.ResultSet;
import java.sql.SQLException;

import cn.josh.bean.Ec_user;
import cn.josh.dao.Ec_userDao;
import cn.josh.utill.JDBCUtil;

public class Ec_userDaoImpl implements Ec_userDao
{

	@Override
	public int register(Ec_user user) 
	{
		String sql = "insert into ec_user(LOGIN_NAME,PASSWORD,NAME,SEX,EMAIL,PHONE,ADDRESS,ROLE)\r\n"
				+	"values(?,?,?,?,?,?,?,?);";

		String[] info = {
							user.getLogin_name() ,user.getPassword() ,user.getName(),
							user.getSex() ,user.getEmail() ,user.getPhone() ,user.getAddress(),
							user.getRole()
						};

		return JDBCUtil.executeUpdate(sql, info);
	}

	@Override
	public Ec_user login(Ec_user user) 
	{
		String sql = "select * from ec_user where login_name = ?;";
		
		String[] info = {user.getLogin_name()};
		
		ResultSet rs = JDBCUtil.executeQuery(sql, info);
		
		try 
		{
			if(rs.next())
			{
				Ec_user user_db = new Ec_user();
				
				String userName = rs.getString("login_name");
				String password = rs.getString("password");
				String name = rs.getString("name");
				String userId = rs.getString("id");
				
				user_db.setId(userId);
				user_db.setLogin_name(userName);
				user_db.setPassword(password);
				user_db.setName(name);
				
				System.out.println("查询到用户");
				return user_db;
				
			}
		}
		catch (SQLException e) 
		{
			System.out.println("没有查询到用户");
			e.printStackTrace();
		}
		
		return null;
	}

	
}
